/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main.util;

/**
 *
 * @author Ariman
 */
import java.util.ArrayList;
import java.util.List;
import main.DBmodels.User;
import main.DBmodels.StatisticRow;
import main.prefExceptions.DAOException;
import main.util.CriptMD5;
import main.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.criterion.*;

public class DAO {

    public DAO() {
    }

    public Long addUser(User person) throws DAOException {
        Long result;
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        List persons = sess.createCriteria(User.class).add(Restrictions.like("login", person.getLogin())).list();
        sess.getTransaction().commit();
        if (!persons.isEmpty()) {
            throw new DAOException("Такое имя уже используеться!");
        } else {
            sess = HibernateUtil.getSessionFactory().getCurrentSession();
            sess.beginTransaction();
            result = (Long) sess.save(person);
            sess.getTransaction().commit();
            addStatisticForUser(person, new StatisticRow(0, 0, 0, 0));
        }

        return result;
    }

    public void updateUser(User person) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.saveOrUpdate(person);
        session.getTransaction().commit();
    }

    public void removeUser(User person) throws DAOException {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.delete(person);
        session.getTransaction().commit();
        removeStatisticOfUser(person);
    }

    public User validateUser(String strLogin, String password)
            throws java.sql.SQLException {

        User obj = null;
        password = CriptMD5.criptString(password);
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        List persons = sess.createCriteria(User.class).add(Restrictions.like("login", strLogin)).add(Restrictions.like("password", password)).list();
        sess.getTransaction().commit();
        if (!persons.isEmpty()) {
            obj = (User) persons.get(0);
        } else {
            obj = null;
        }
        return obj;
    }

    public User getUserByUID(Long UID)
            throws java.sql.SQLException, DAOException {

        User obj = null;
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        List persons = sess.createCriteria(User.class).add(Restrictions.like("UID", UID)).list();
        sess.getTransaction().commit();
        if (!persons.isEmpty()) {
            obj = (User) persons.get(0);
        } else {
            obj = null;
            throw new DAOException("Нет такого ID!");
        }
        return obj;
    }

    public User getUserByName(String name)
            throws java.sql.SQLException {

        User obj = null;
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        List persons = sess.createCriteria(User.class).add(Restrictions.like("login", name)).list();
        sess.getTransaction().commit();
        if (!persons.isEmpty()) {
            obj = (User) persons.get(0);
        } else {
            obj = null;
        }
        return obj;
    }

    public User getUserByEmail(String email)
            throws java.sql.SQLException {

        User obj = null;
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        List persons = sess.createCriteria(User.class).add(Restrictions.like("email", email)).list();
        sess.getTransaction().commit();
        if (!persons.isEmpty()) {
            obj = (User) persons.get(0);
        } else {
            obj = null;
        }
        return obj;
    }

    public Long addStatisticForUser(User person, StatisticRow stat) {
        stat.setUID(person.getUID());
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Long result = (Long) session.save(stat);
        session.getTransaction().commit();
        return result;
    }

    public void updateStatisticForUser(User person, StatisticRow stat) {
        stat.setUID(person.getUID());
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.saveOrUpdate(stat);
        session.getTransaction().commit();
    }

    public void updateStatisticForUID(Long UID, StatisticRow stat) {
        stat.setUID(UID);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.saveOrUpdate(stat);
        session.getTransaction().commit();
    }

    public void removeStatisticOfUser(User person) throws DAOException {
        StatisticRow stat = new StatisticRow();
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        List persons = sess.createCriteria(StatisticRow.class).add(Restrictions.like("UID", person.getUID())).list();
        sess.getTransaction().commit();
        if (!persons.isEmpty()) {
            stat = (StatisticRow) persons.get(0);
            sess = HibernateUtil.getSessionFactory().getCurrentSession();
            sess.beginTransaction();
            sess.delete(stat);
            sess.getTransaction().commit();
        } else {
            stat = null;
            throw new DAOException("User not found");
            // wibrosit' eksepwen ob otsutstvii
        }
    }

    public ArrayList<StatisticRow> getAllStatistic() throws java.sql.SQLException {
        Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
        sess.beginTransaction();
        ArrayList allStatisticRow = (ArrayList) sess.createCriteria(StatisticRow.class).list();
        sess.getTransaction().commit();
        return allStatisticRow;
    }
}
